home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / recover2.c < prev    next >
C/C++ Source or Header  |  1998-06-14  |  9KB  |  321 lines

  1. /*
  2.  *                              Recover2.c
  3.  *
  4.  * Rebuilds trashed CTDLROOM.SYS.
  5.  */
  6.  
  7. #include "ctdl.h"    /* header file  */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <fctype.h>
  12. #include <math.h>
  13. #include <ctype.h>
  14. #include <time.h>
  15. #include <proto/exec.h>
  16. #include <dos/dos.h>
  17. #include <pragmas/dos_pragmas.h>
  18. #include "exec/memory.h"
  19. #include "exec/ports.h"
  20. #include "exec/exec.h"
  21.  
  22.  
  23. /*
  24.  *                              History
  25.  *
  26.  * 87Oct11 HAW  V3 update.
  27.  * 85Nov16 HAW  Modified for MS-DOS libraries.
  28.  * 85Apr15 HAW  Move to MS-DOS.
  29.  * 84Aug16 HAW  Now no need to reconfigure afterwards, gives stats.
  30.  * 84Aug15 HAW  Documented.
  31.  * 84Aug15 HAW  Created (In the wee hours).
  32.  */
  33.  
  34. /*
  35.  *                              Contents
  36.  *
  37.  *      crashout()              irrecoverable error
  38.  *      doRest()                Read msgs.
  39.  *      handle()                Inserts msg into room slot, etc.
  40.  *      indexRooms()            build RAM index to ctdlroom.sys
  41.  *      init()                  Open up files for recovery effort.
  42.  *      main()                  Master control.
  43.  *      noteRoom()              enter room into RAM index
  44.  *      XzapRoomFile()          Zaps room file for you.
  45.  */
  46.  
  47. extern CONFIG   cfg;    /* The main variable to be saved        */
  48. extern MessageBuffer msgBuf;    /* The -sole- message buffer    */
  49. extern FILE     *msgfl, *msgfl2; /* file descriptor for the msg file    */
  50. extern FILE             *upfd;
  51. extern rTable   *roomTab;       /* RAM index of rooms   */
  52. extern aRoom    roomBuf;        /* room buffer  */
  53. extern FILE     *roomfl;        /* file descriptor for rooms    */
  54. extern int      thisRoom;       /* room currently in roomBuf    */
  55. FILE            *netLog=stdout;
  56.  
  57. FILE *fopen();
  58. char handle(MSG_NUMBER mess);
  59. void init(void);
  60. void XzapRoomFile(void);
  61. void doRest(void);
  62. int  mPrintf(char *format, ...) {return 0; }  /* stub to quiet the linker */
  63.  
  64. /*
  65.  * crashout()
  66.  *
  67.  * This handles the irrecoverable error.
  68.  */
  69. void crashout(str)
  70. char *str;
  71. {
  72.     exit(printf(str));
  73. }
  74.  
  75. /*
  76.  * doRest()
  77.  *
  78.  * This function loops thru the msg file until finished, reconstructing the
  79.  * rooms and their contents from the messages found.
  80.  */
  81. void doRest()
  82. {
  83.     MSG_NUMBER message, firstMessage;
  84.     int total, mailCount;       /* For stat keeping. */
  85.     extern struct mBuf mFile1;
  86.  
  87.     startAt(msgfl, &mFile1, 0, 0);
  88.     getMessage(getMsgChar, FALSE, FALSE, TRUE);
  89.     message = atol(msgBuf.mbId);
  90.     printf("%ld\n", message);
  91.     firstMessage = message;
  92.     mailCount = 0;
  93.     if (handle(message)) mailCount++;
  94.     getMessage(getMsgChar, FALSE, FALSE, TRUE);
  95.     message = atol(msgBuf.mbId);
  96.     total = 1;
  97.     while (message != firstMessage) {
  98.         total++;
  99.         printf("%ld\n", message);
  100.         if (handle(message)) mailCount++;
  101.         getMessage(getMsgChar, FALSE, FALSE, TRUE);
  102.         message = atol(msgBuf.mbId);
  103.     }
  104.     printf("We have %u messages, with %u of them being in the mail.\n",
  105.                                                 total, mailCount);
  106. }
  107.  
  108. /*
  109.  * handle()
  110.  *
  111.  * This function handles setting up and saving room indexes, etc, based on the
  112.  * current message.
  113.  */
  114. char handle(mess)
  115. MSG_NUMBER mess;
  116. {
  117.     int  i, j, k;
  118.  
  119.     if (msgBuf.mbroom[0] == '\0') return FALSE; /* Can't handle */
  120.     printf("Message for %s\n", msgBuf.mbroom);
  121.     for (i = 0; i < MAXROOMS; i++)
  122.         if (roomTab[i].rtname[0] == 0) break;
  123.         else if (strCmpU(roomTab[i].rtname, msgBuf.mbroom) == 0) break;
  124.  
  125.     if (i == MAXROOMS) return FALSE;    /* If no room left in room      */
  126.     if (i == MAILROOM) return TRUE;     /* file or room is Mail don't do*/
  127.                                         /* anything.                    */
  128.     printf("Being put in slot %d\n", i);
  129.     getRoom(i);                         /* Get appropriate slot */
  130.     strcpy(roomTab[i].rtname, msgBuf.mbroom);   /* Copy whether needs it*/
  131.     strcpy(roomBuf.rbname, msgBuf.mbroom);
  132.     if (roomBuf.rbflags.INUSE == FALSE)
  133.         for (j = 0; j < MSGSPERRM - 1; j++)
  134.             roomBuf.msg[j].rbmsgNo = 0l;
  135.     roomBuf.rbflags.INUSE = TRUE;
  136.     for (k = 0; roomBuf.msg[k+1].rbmsgNo < mess && k < MSGSPERRM-1; k++)
  137.         ;
  138.     for (j = 0; j < k; j++) {   /* Move msgs up 1.      */
  139.         roomBuf.msg[j].rbmsgLoc = roomBuf.msg[j+1].rbmsgLoc;
  140.         roomBuf.msg[j].rbmsgNo  = roomBuf.msg[j+1].rbmsgNo;
  141.     }
  142.     roomBuf.msg[k].rbmsgNo  = mess;/* And now insert msg    */
  143.     roomBuf.msg[k].rbmsgLoc = msgBuf.mbheadSector;
  144.     putRoom(i); /* And save our folly. */
  145.     return FALSE;
  146. }
  147.  
  148. /*
  149.  * indexRooms()
  150.  *
  151.  * This will build a RAM index to CTDLROOM.SYS, and delete empty rooms.
  152.  */
  153. void indexRooms()
  154. {
  155.     int  goodRoom, m, roomCount, slot;
  156.  
  157.     roomCount = 0;
  158.     for (slot = 0;  slot < MAXROOMS;  slot++) {
  159.         getRoom(slot);
  160.         if (roomBuf.rbflags.INUSE == 1) {
  161.             roomBuf.rbflags.INUSE = 0;          /* clear "inUse" flag */
  162.             for (m = 0, goodRoom = FALSE; m < MSGSPERRM && !goodRoom; m++) {
  163.                 if (roomBuf.msg[m].rbmsgNo > cfg.oldest) {
  164.                     goodRoom    = TRUE;
  165.                 }
  166.             }
  167.             if (goodRoom   || roomBuf.rbflags.PERMROOM == 1)   {
  168.                 roomBuf.rbflags.INUSE = 1;
  169.             }
  170.  
  171.             if (roomBuf.rbflags.INUSE == 1)
  172.                 roomCount++;
  173.             else {
  174.                 roomBuf.rbflags.INUSE    = 0;
  175.                 roomBuf.rbflags.ISDIR   = 0;
  176.                 roomBuf.rbflags.PERMROOM = 0;
  177.                 roomBuf.rbflags.INUSE    = 0;
  178.                 putRoom(slot);
  179.             }
  180.         }
  181.         noteRoom();
  182.     }
  183. }
  184.  
  185. /*
  186.  * init()
  187.  *
  188.  * This function will set up the files for recovery.
  189.  */
  190. void init()
  191. {
  192.     char c;
  193.     SYS_FILE temp;
  194.     extern char *R_W_ANY, *W_R_ANY;
  195.  
  196.     cfg.weAre = UTILITY;
  197.     if (!readSysTab(TRUE, TRUE)) crashout("\nDisaster!  I need CTDLTABL.SYS!");
  198.     cfg.weAre = UTILITY;
  199.     mvToHomeDisk(&cfg.homeArea);
  200.  
  201.     if (access(LOCKFILE, 0) != ERROR) {
  202.         printf("Please do not run Recover2 using Outside Commands.\n");
  203.         writeSysTab();
  204.         exit(1);
  205.     }
  206.     /* open message file */
  207.  
  208.     InitMsgBase();
  209.  
  210.     makeSysName(temp, "ctdlroom.sys", &cfg.roomArea);
  211.     /* open room file */
  212.     if ((roomfl = fopen(temp, R_W_ANY)) == NULL) {
  213.         if ((roomfl = fopen(temp, W_R_ANY)) == NULL)
  214.             exit(printf("Hey! Can't create the room file?"));
  215.     }
  216.     initRoomBuf(&roomBuf);
  217.     strcpy(roomTab[0].rtname, cfg.codeBuf + cfg.bRoom);
  218.     strcpy(roomTab[1].rtname, "Mail");
  219.     strcpy(roomTab[2].rtname, "Aide");
  220.     for (c = 3; c < MAXROOMS; c++)
  221.         roomTab[c].rtname[0] = 0;       /* Convenient way to zap roomTab */
  222. }
  223.  
  224. /*
  225.  * main()
  226.  *
  227.  * This directs salvage proceedings and collects profits.
  228.  */
  229. int main(void);
  230. int main()
  231. {
  232.     printf("Citadel Room Recover II %s - %s\n\n", VERSION_NAME, COPYRIGHT);
  233.     init();
  234.     puts("No error msgs.  Go off and do something for an hour (or 2).\n");
  235.     XzapRoomFile();
  236.     doRest();
  237.     indexRooms();
  238.     writeSysTab();
  239.     printf("Don't need to reconfigure.\n");
  240.     return 0;
  241. }
  242.  
  243. /*
  244.  * noteRoom()
  245.  *
  246.  * This function will enter a room into RAM index array.
  247.  */
  248. void noteRoom()
  249. {
  250.     int   i;
  251.     MSG_NUMBER last;
  252.  
  253.     last = 0l;
  254.     for (i = 0;  i < MSGSPERRM;  i++)  {
  255.         if (roomBuf.msg[i].rbmsgNo > last) {
  256.             last = roomBuf.msg[i].rbmsgNo;
  257.         }
  258.     }
  259.     roomTab[thisRoom].rtlastMessage = last      ;
  260.     strCpy(roomTab[thisRoom].rtname, roomBuf.rbname) ;
  261.     roomTab[thisRoom].rtgen     = roomBuf.rbgen  ;
  262.     roomTab[thisRoom].rtflags.INUSE    = roomBuf.rbflags.INUSE;
  263.     roomTab[thisRoom].rtflags.PUBLIC   = roomBuf.rbflags.PUBLIC;
  264.     roomTab[thisRoom].rtflags.ISDIR    = roomBuf.rbflags.ISDIR;
  265.     roomTab[thisRoom].rtflags.PERMROOM = roomBuf.rbflags.PERMROOM;
  266.     roomTab[thisRoom].rtflags.SKIP      = roomBuf.rbflags.SKIP;
  267.     roomTab[thisRoom].rtFlIndex = roomBuf.rbFlIndex;
  268. }
  269.  
  270. /*
  271.  * XzapRoomFile()
  272.  *
  273.  * This function erases and re-initializes ctdlroom.sys.
  274.  */
  275. void XzapRoomFile()
  276. {
  277.     int i;
  278.  
  279.     roomBuf.rbflags.PUBLIC   = TRUE;    /* This should be safe  */
  280.     roomBuf.rbflags.INUSE    = FALSE;
  281.     roomBuf.rbflags.ISDIR = FALSE;
  282.     roomBuf.rbflags.PERMROOM = FALSE;
  283.     roomBuf.rbgen       = 0;
  284.     roomBuf.rbname[0]   = 0;    /* unnecessary -- but I like it...      */
  285.     roomBuf.rbFlIndex   = 0;
  286.     for (i=0;  i<MSGSPERRM;  i++) {
  287.         roomBuf.msg[i].rbmsgNo = 0l;
  288.         roomBuf.msg[i].rbmsgLoc = 0;
  289.     }
  290.  
  291.     printf("maxrooms=%d\n", MAXROOMS);
  292.  
  293.     for (i=0;  i<MAXROOMS;  i++) {
  294.         RoomSys(i);
  295.         printf("clearing room %d\n", i);
  296.         putRoom(i);
  297.     }
  298.  
  299.     /* Lobby> always exists -- guarantees us a place to stand! */
  300.     thisRoom    = 0     ;
  301.     strcpy(roomBuf.rbname, cfg.codeBuf + cfg.bRoom)     ;
  302.     roomBuf.rbflags.PERMROOM = TRUE;
  303.     roomBuf.rbflags.PUBLIC   = TRUE;
  304.     roomBuf.rbflags.INUSE    = TRUE;
  305.     RoomSys(LOBBY);
  306.     putRoom(LOBBY);
  307.  
  308.     /* Mail> is also permanent...       */
  309.     thisRoom    = MAILROOM;
  310.     strcpy(roomBuf.rbname, "Mail");
  311.     RoomSys(MAILROOM);
  312.     putRoom(MAILROOM);
  313.  
  314.     /* Aide> also...    */
  315.     thisRoom    = AIDEROOM;
  316.     strcpy(roomBuf.rbname, "Aide");
  317.     roomBuf.rbflags.PUBLIC = FALSE;
  318.     RoomSys(AIDEROOM);
  319.     putRoom(AIDEROOM);
  320. }
  321.